<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        document.cookie = 'name=xiaohong';
        // max-age=500设置过期时间 500秒
        document.cookie = 'name1=xiaohong;max-age=500'
        var manageCookies = {
            set: function(key, value, expTime) {
                document.cookie = key + '=' + value + ';' + 'max-age' + '=' + expTime
                return this
            },
            delete: function(key) {
                // 将时间设置为负数，可以将cookie中属性删除
                return this.set(key, '', '-1')
            },
            get: function(key) {
                var CookiesArray = document.cookie.split(';'),
                    CookiesObject = {};
                console.log(document.cookie);
                console.log(CookiesArray);
                for (var i = 0; i < CookiesArray.length; i++) {
                    var cookieItem = CookiesArray[i];
                    var cookieItemArray = cookieItem.split('=')
                    console.log(cookieItemArray);
                    CookiesObject[cookieItemArray[0].trim()] = cookieItemArray[1]
                }
                console.log(CookiesObject);
                return CookiesObject[key]
            }
        }
        manageCookies.set('name2', 'xiaoh', 5)
        manageCookies.delete('name2')
        console.log(manageCookies.get('name'));
    </script>
</body>

</html>